﻿/* 功能:  订单管理
 * 创建人：Kencery  创建时间：2017-12-9
 */
var ml = window.ml || {};
ml.admin = ml.admin || {};

ml.admin.order = (function () {
    var defaults = {
        listUrl: "/Order/List",
        detailUrl: "/Order/GetDetailById",
        getOrderStateUrl: "/Common/GetOrderState"
    };
    var opt = {};

    var columns = [{
        field: 'state',
        radio: true
    }, {
        title: '行号',
        field: 'number',
        align: 'center',
        hide: true,
        formatter: plscommon.tableNumber
    }, {
        title: '订单ID',
        field: 'Id',
    }, {
        title: '商户名称',
        field: 'Name',
        align: 'center',
    }, {
        title: '用户名/流水号',
        field: 'NickName',
        align: 'center',
        formatter: function (value, rows, index) {
            return rows.NickName + "</br>" + rows.OrderNumber;
        }
    }, {
        title: '总价',
        field: 'Price',
        align: 'center',
    }, {
        title: '优惠价',
        field: 'DiscountPrice',
        align: 'center',
    }, {
        title: '实付价',
        field: 'ActPayPrice',
        align: 'center',
    }, {
        title: '人民币',
        field: 'ActPayRMB',
        align: 'center',
    }, {
        title: '订单/支付状态',
        field: 'State',
        align: 'center',
        formatter: function (value, rows, index) {
            var payState = returnPayStatue(rows.PayState);
            return rows.StateName + "</br>" + payState;
        }
    }, {
        title: '下单时间',
        field: 'CreateTime',
        align: 'center',
    }, {
        title: '订单备注',
        field: 'Description',
        align: 'center',
    }, {
        title: '用餐人数',
        field: 'NumberOfMeals',
        align: 'center',
    }];

    var initTable = function () {
        plscommon.bootstraptable({
            id: "#orderinfo",
            url: defaults.listUrl,
            queryParams: queryParams,
            uniqueId: "Id",
            columns: columns
        });
    }

    var queryParams = function (params) {
        return {
            offset: params.offset,             //后台计算显示数据信息
            pagesize: params.limit,            //每页显示多少行
            merchantname_search: $("#merchantname_search").val(),
            ordernumber_search: $("#ordernumber_search").val(),
            state_search: $("#state_search").val(),
            paystate_search: $("#paystate_search").val()
        };
    };

    var initDropDownList = function () {
        plscommon.dropDownList("#state_search", defaults.getOrderStateUrl, 1, 1);
    }

    var clickEvent = function () {
        $("#btnQueryList").on("click", function () { btnQueryList(); });        //按条件查询结果
        $("#btnReset").on("click", function () { btnReset(); });                //清空文本框信息

        $("#btnDetail").on("click", function () { btnDetail(); });              //订单详情
    };

    var btnQueryList = function () {
        plscommon.refreshTable("orderinfo");
    }

    var btnReset = function () {
        plscommon.resetFrom("formSearch");
    }

    var btnDetail = function () {
        //必须选中一条才能弹出
        var data = plscommon.dialogWaring('orderinfo', 'DetailModal', 'orderDetail');
        if (!data.Id) {
            return false;
        }

        plscommon.ajax({
            url: defaults.detailUrl,
            type: "GET",
            data: { id: data.Id },
            success: function () {
                var data = this.data;
                $("#detail_orderumber").val(data.OrderNumber);
                $("#detail_nickname").val(data.NickName);
                $("#detail_merchant_name").val(data.Name);
                $("#detail_price").val(data.Price);
                $("#detail_discountprice").val(data.DiscountPrice);
                $("#detail_actpayprice").val(data.ActPayPrice);
                $("#detail_actpay_rmb").val(data.ActPayRMB);

                if (!data.ContactName) {
                    $("#detail_address").val("");
                } else {
                    $("#detail_address").val("收货人名称：" + data.ContactName + "，收货人电话：" + data.Contact + "，收货人地址：" + data.Address);
                }
                $("#detail_paymethod").val(data.PayMethod);
                $("#detail_numberofmeals").val(data.NumberOfMeals);
                $("#detail_redpackagetype").val(data.RedPackageType);
                $("#detail_description").val(data.Description);
                $("#detail_state").val(data.StateName);
                $("#detail_paystate").val(returnPayStatue(data.PayState));
                $("#detail_createtime").val(data.CreateTime);
                $("#detail_servicetime").val(data.ServiceTime);
                $("#detail_numberOfMeals").val(data.NumberOfMeals);

                $('#tbody').empty();
                if (data.orderDetail != null) {
                    var html = "";
                    for (var i = 0; i < data.orderDetail.length; i++) {
                        var result = data.orderDetail[i];
                        html += '<tr><td>' + result.Name + '</td><td>' + result.ProductNumber + '</td><td>' + result.ProductPrice + '</td></tr>';
                    }
                    $('#tbody').append(html);
                }
            }
        });
    }

    var returnPayStatue = function (value) {
        var result = "";
        if (value == 0) {
            result = "未支付";
        }
        if (value == 1) {
            result = "已支付";
        }
        return result;
    }

    return {
        init: function (options) {
            $.extend(defaults, options || {});
            initTable();            //初始化Table表格
            clickEvent();           //触发事件
            initDropDownList();     //初始化下拉事件
        }
    };
}());